package com.jsyxdl.listener;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.basic.entity.BaseObject;
import com.hustcad.plm.caddocapi.service.TyplmCADRelObjectService;
import com.hustcad.plm.caddocapi.service.cadstruct.TyplmCadStructService;
import com.hustcad.plm.pdm.caddoc.model.dto.cadstruct.CadStructConditionDTO;
import com.hustcad.plm.pdm.caddoc.model.dto.cadstruct.CadStructFilterDTO;
import com.hustcad.plm.pdm.caddoc.model.vo.cadstruct.CadStructFilterVO;
import com.hustcad.plm.pdm.common.model.dto.search.SearchConditionDTO;
import com.hustcad.plm.pdm.common.model.vo.search.ResultEntityVO;
import com.hustcad.plm.pdm.doc.service.TyplmDocumentService;
import com.hustcad.plm.pdm.doc.vo.DocDetails;
import com.hustcad.plm.pdm.ec.module.dto.ECAddChangeObjectDTO;
import com.hustcad.plm.pdm.ec.module.dto.ECObjDTO;
import com.hustcad.plm.pdm.ec.module.dto.ECObjData;
import com.hustcad.plm.pdm.ec.module.dto.EcAfterAddReturnDTO;
import com.hustcad.plm.pdm.ec.module.vo.ECObjDataVO;
import com.hustcad.plm.pdm.ec.service.TyplmEngineeringChangeService;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowChgFormView;
import com.hustcad.plm.pdm.flowbusiness.model.entity.FlowObject;
import com.hustcad.plm.pdm.flowbusiness.model.entity.WorkFlowObjectPackage;
import com.hustcad.plm.pdm.flowbusiness.model.vo.FlowObjectStructuredParamVO;
import com.hustcad.plm.pdm.flowbusiness.model.vo.FlowObjectStructuredReturnVO;
import com.hustcad.plm.pdm.flowbusiness.service.TyplmWorkFlowObjectPackageService;
import com.hustcad.plm.pdm.lifestage.model.dto.EntityLifeCycleDTO;
import com.hustcad.plm.pdm.middle.partbom.service.TyplmPdmPartRelObjectService;
import com.hustcad.plm.pdm.middle.partbom.service.TyplmPdmPartService;
import com.hustcad.plm.pdm.partbom.dto.ChildPartsFiltersDTO;
import com.hustcad.plm.pdm.partbom.dto.PartBomTree;
import com.hustcad.plm.pdm.partbom.service.TyplmPartBomService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartDescribeLinkService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartRelObjectService;
import com.hustcad.plm.pdm.partbom.service.TyplmPartService;
import com.hustcad.plm.pdm.partbom.vo.PartBomTreeByFilterVO;
import com.hustcad.plm.pdm.partbom.vo.PartDetailsVO;
import com.hustcad.plm.pdm.partbom.vo.QueryDescribeObjVO;
import com.hustcad.plm.pdm.workflow.annotion.CustomListener;
import com.hustcad.plm.pdm.workflow.annotion.CustomListenerField;
import com.hustcad.plm.pdm.workflow.model.entity.WorkFlowContainerView;
import com.hustcad.plm.pdm.workflow.service.TyplmWorkFlowContainerService;
import com.ty.basic.entity.IdentifierEntity;
import com.ty.basic.exception.MessageException;
import com.ty.basic.exception.message.SystemErrorCodeEnum;
import com.ty.basic.handler.exceptionhandler.exception.TyException;
import org.apache.commons.lang3.StringUtils;
import org.flowable.bpmn.model.BaseElement;
import org.flowable.bpmn.model.FlowElementsContainer;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.ExecutionListener;
import org.flowable.engine.impl.el.FixedValue;
import org.flowable.engine.impl.persistence.entity.ExecutionEntityImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CustomListener(
        category = "其他"
        , description = "自动收集相关对象到工作流"
)
public class AutoCollectObject2WorkFlow implements ExecutionListener {
    private static final Logger log = LoggerFactory.getLogger(AutoCollectObject2WorkFlow.class);


    @CustomListenerField(
            description = "所有子件加入流程",
            defaultValue = "",
            required = true
    )
    private FixedValue subComponent;
    @CustomListenerField(
            description = "子件关联的参考文档加入流程",
            defaultValue = "",
            required = true
    )
    private FixedValue referencedDocument;
    @CustomListenerField(
            description = "子件的父件加入流程",
            defaultValue = "",
            required = true
    )
    private FixedValue accessoriesSubComponents;

    @CustomListenerField(
            description = "主控图纸加入流程",
            defaultValue = "",
            required = true
    )
    private FixedValue mainControlDrawing;
    @CustomListenerField(
            description = "非主控图纸加入流程",
            defaultValue = "",
            required = true
    )
    private FixedValue monControlDrawing;

    @CustomListenerField(
            description = "变更前图纸关联零部件加入流程",
            defaultValue = "",
            required = true
    )
    private FixedValue drawingPart;
    @CustomListenerField(
            description = "变更后图纸关联零部件加入流程",
            defaultValue = "",
            required = true
    )
    private FixedValue afterDrawingPart;

    @CustomListenerField(
            description = "对象状态",
            defaultValue = "",
            required = true
    )
    private FixedValue statusObj;

    @CustomListenerField(
            description = "添加位置",
            defaultValue = "",
            required = true
    )
    private FixedValue addPosition;

    @Override
    public void notify(DelegateExecution execution) {
        try {
            String workFlowContainerIdStr = execution.getProcessInstanceBusinessKey();
            if (StringUtils.isBlank(workFlowContainerIdStr)) {
                workFlowContainerIdStr = execution.getVariable("workFlowContainerId").toString();
            }
            String workflowId=null;
            Object processDefinitionKey = ((ExecutionEntityImpl) execution).getProcessDefinitionKey();
            if(processDefinitionKey==null){
                FlowElementsContainer flowElementsContainer =execution.getCurrentFlowElement().getParentContainer();
                if(flowElementsContainer instanceof BaseElement){
                    BaseElement baseElement= (BaseElement) flowElementsContainer;
                    workflowId=baseElement.getId();
                }
            }
            else{
                workflowId = processDefinitionKey.toString();

            }

            if(workflowId!=null){
                if (workflowId.startsWith("M")) {
                    workflowId = workflowId.substring(1);
                }
                BigInteger workFlowContainerId = new BigInteger(workFlowContainerIdStr);
                BusinessProcessing(workFlowContainerId, workflowId);
            }
            else{
                BigInteger workFlowContainerId = new BigInteger(workFlowContainerIdStr);
                BusinessProcessing(workFlowContainerId, null);
            }

        } catch (Exception e) {
            if(e instanceof TyException){
                TyException tyException= (TyException) e;
                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(tyException.getMessage());
            }
            else if(e instanceof MessageException){
                MessageException tyException= (MessageException) e;
                throw SystemErrorCodeEnum.WORK_FLOW_CONTENT_ERROR.getException(tyException.getErrorMsg());
            }
            else{
                try {
                    throw e;
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            }
        }
    }


    /***
     * 获取收集领部件对象
     * @param workFlowContainerId
     * @return
     */
    public List<PartDetailsVO> collectMasterObject(BigInteger workFlowContainerId) {
        TyplmWorkFlowContainerService typlmWorkFlowContainerService = SpringUtil.getBean(
                TyplmWorkFlowContainerService.class);
        TyplmPartService typlmPartService = SpringUtil.getBean(
                TyplmPartService.class);
        WorkFlowContainerView workFlowContainerView = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(
                workFlowContainerId);

        List<PartDetailsVO> partDetailsVOList = new ArrayList<>();
        if (workFlowContainerView instanceof WorkFlowObjectPackage) {
            WorkFlowObjectPackage workFlowObjectPackage = (WorkFlowObjectPackage) workFlowContainerView;
            if (workFlowObjectPackage.getFlowObjectList() != null && !workFlowObjectPackage.getFlowObjectList()
                    .isEmpty()) {
                for (FlowObject flowObject : workFlowObjectPackage.getFlowObjectList()) {

                    BigInteger partOid = flowObject.getRefObjectOid();
                    String oType = flowObject.getRefObjectOtype();
                    if ("ty.inteplm.part.CTyPart".equals(oType) == false) {
                        continue;
                    }
                    PartDetailsVO partDetailsVO = typlmPartService.queryPartDetails(partOid, oType);
                    if ("成品".equals(partDetailsVO.getTypeName()) == false) {
                        continue;
                    }
                    if ("ty.inteplm.part.CTyPart".equals(partDetailsVO.getOtype())) {
                        partDetailsVOList.add(partDetailsVO);
                    }
                }
            }
        }
        if (workFlowContainerView instanceof FlowChgFormView) {
            FlowChgFormView flowChgFormView = (FlowChgFormView) workFlowContainerView;
            if(flowChgFormView.getChgFormVO().getEcAffectVOS()!=null){
                for (int i = 0; i < flowChgFormView.getChgFormVO().getEcAffectVOS().size(); i++) {
                    ECObjData ecObjData = flowChgFormView.getChgFormVO().getEcAffectVOS().get(i);
                    if ("成品".equals(ecObjData.getTypeDefName()) == false) {
                        continue;
                    }
                    BigInteger partOid = ecObjData.getRefObjectOid();
                    String oType = ecObjData.getRefObjectOtype();
                    if ("ty.inteplm.part.CTyPart".equals(oType) == false) {
                        continue;
                    }

                    PartDetailsVO partDetailsVO = typlmPartService.queryPartDetails(partOid, oType);

                    if ("ty.inteplm.part.CTyPart".equals(partDetailsVO.getOtype())) {
                        partDetailsVOList.add(partDetailsVO);
                    }
                }
            }
        }
        return partDetailsVOList;
    }
    public List<DocDetails> collectMasterDocDetails(BigInteger workFlowContainerId) {
        TyplmWorkFlowContainerService typlmWorkFlowContainerService = SpringUtil.getBean(
                TyplmWorkFlowContainerService.class);

        TyplmDocumentService typlmDocumentService = SpringUtil.getBean(
                TyplmDocumentService.class);


        WorkFlowContainerView workFlowContainerView = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(
                workFlowContainerId);

        List<DocDetails> partDetailsVOList = new ArrayList<>();
        if (workFlowContainerView instanceof WorkFlowObjectPackage) {
            WorkFlowObjectPackage workFlowObjectPackage = (WorkFlowObjectPackage) workFlowContainerView;
            if (workFlowObjectPackage.getFlowObjectList() != null && !workFlowObjectPackage.getFlowObjectList()
                    .isEmpty()) {
                for (FlowObject flowObject : workFlowObjectPackage.getFlowObjectList()) {

                    BigInteger partOid = flowObject.getRefObjectOid();
                    String oType = flowObject.getRefObjectOtype();
                    if ("ty.inteplm.cad.CTyCADDoc".equals(oType) == false) {
                        continue;
                    }
                    DocDetails docDetails = typlmDocumentService.queryDocDetails(partOid, oType);

                    if ("ty.inteplm.cad.CTyCADDoc".equals(docDetails.getOtype())) {
                        partDetailsVOList.add(docDetails);
                    }
                }
            }
        }
        if (workFlowContainerView instanceof FlowChgFormView) {
            FlowChgFormView flowChgFormView = (FlowChgFormView) workFlowContainerView;
            if(this.afterDrawingPart!=null){
                if("是".equals(this.afterDrawingPart.getExpressionText())){
                    if(flowChgFormView.getChgFormVO().getEcAffectVOS()!=null){
                        for (int i = 0; i < flowChgFormView.getChgFormVO().getEcAffectVOS().size(); i++) {
                            ECObjData ecObjData = flowChgFormView.getChgFormVO().getEcAffectVOS().get(i);
                            if ("成品".equals(ecObjData.getTypeDefName()) == false) {
                                continue;
                            }
                            BigInteger partOid = ecObjData.getRefObjectOid();
                            String oType = ecObjData.getRefObjectOtype();
                            if ("ty.inteplm.cad.CTyCADDoc".equals(oType) == false) {
                                continue;
                            }

                            DocDetails docDetails = typlmDocumentService.queryDocDetails(partOid, oType);

                            if ("ty.inteplm.cad.CTyCADDoc".equals(docDetails.getOtype())) {
                                partDetailsVOList.add(docDetails);
                            }
                        }
                    }
                }
            }
            if(this.drawingPart!=null){
                if("是".equals(this.drawingPart.getExpressionText())){
                    if(flowChgFormView.getChgFormVO().getEcObjDataVOS()!=null){
                        for (int i = 0; i < flowChgFormView.getChgFormVO().getEcObjDataVOS().size(); i++) {
                            ECObjData ecObjData = flowChgFormView.getChgFormVO().getEcObjDataVOS().get(i);
                            BigInteger partOid = ecObjData.getRefObjectOid();
                            String oType = ecObjData.getRefObjectOtype();
                            if ("ty.inteplm.cad.CTyCADDoc".equals(oType) == false) {
                                continue;
                            }

                            DocDetails docDetails = typlmDocumentService.queryDocDetails(partOid, oType);

                            if ("ty.inteplm.cad.CTyCADDoc".equals(docDetails.getOtype())) {
                                partDetailsVOList.add(docDetails);
                            }
                        }
                    }
                }
            }

        }
        return partDetailsVOList;
    }



    public int BusinessProcessing(BigInteger workFlowContainerId, String workflowId) throws Exception {

        TyplmWorkFlowContainerService typlmWorkFlowContainerService = SpringUtil.getBean(
                TyplmWorkFlowContainerService.class);


        WorkFlowContainerView tworkFlowContainerView = typlmWorkFlowContainerService.getWorkFlowContainerDetailByPrimaryKey(
                workFlowContainerId);
        if (tworkFlowContainerView instanceof WorkFlowObjectPackage) {
            WorkFlowObjectPackage workFlowObjectPackage = (WorkFlowObjectPackage) tworkFlowContainerView;
            if (workFlowObjectPackage.getFlowObjectList() != null && !workFlowObjectPackage.getFlowObjectList()
                    .isEmpty()) {

                if(workflowId!=null){
                    publishCollection(workFlowObjectPackage, workFlowContainerId, workflowId);
                }
            }
        }
        else if (tworkFlowContainerView instanceof FlowChgFormView) {
            FlowChgFormView flowChgFormView = (FlowChgFormView) tworkFlowContainerView;
            return changeCollection(flowChgFormView,workFlowContainerId);
        }

        return 0;
    }
    /**
     * 发布流程收集
     * @param workFlowObjectPackage
     * @param workFlowContainerId
     * @param workflowId
     * @return
     */
    public int publishCollection(WorkFlowObjectPackage workFlowObjectPackage, BigInteger workFlowContainerId,
                                 String workflowId) {
        TyplmPartBomService typlmPartBomService = SpringUtil.getBean(TyplmPartBomService.class);

        TyplmWorkFlowObjectPackageService typlmWorkFlowObjectPackageService = SpringUtil.getBean(
                TyplmWorkFlowObjectPackageService.class);

        TyplmPartService typlmPartService = SpringUtil.getBean(
                TyplmPartService.class);
        TyplmPdmPartService typlmPdmPartService = SpringUtil.getBean(
                TyplmPdmPartService.class);
        TyplmPartDescribeLinkService typlmPartDescribeLinkService = SpringUtil.getBean(
                TyplmPartDescribeLinkService.class);
        TyplmPartRelObjectService typlmPartRelObjectService = SpringUtil.getBean(TyplmPartRelObjectService.class);
        TyplmCADRelObjectService typlmCADRelObjectService = SpringUtil.getBean(TyplmCADRelObjectService.class);
        TyplmPdmPartRelObjectService typlmPdmPartRelObjectService = SpringUtil.getBean(
                TyplmPdmPartRelObjectService.class);
        TyplmCadStructService typlmCadStructService=SpringUtil.getBean(
                TyplmCadStructService.class);


        Map<String, Object> cmd = new HashMap<>();
        for (FlowObject flowObject : workFlowObjectPackage.getFlowObjectList()) {
            cmd.put(flowObject.getObjectNumber(), flowObject);
        }

        List<PartDetailsVO> partDetailsVOList = collectMasterObject(workFlowContainerId);

        List<DocDetails> docDetailsListAll = new ArrayList<>();
        List<JSONObject> referenceDocListAll = new ArrayList<>();
        List<PartDetailsVO> subComponentList = new ArrayList<>();
        List<DocDetails> referenceDocList = new ArrayList<>();
        List<QueryDescribeObjVO> describeObjList = new ArrayList<>();
        List<PartDetailsVO> drawingPartList = new ArrayList<>();

        for (int i = 0; i < partDetailsVOList.size(); i++) {
            PartDetailsVO partDetailsVO = partDetailsVOList.get(i);
            com.hustcad.plm.pdm.partbom.dto.ExpandBomTreeByFilterDTO expandBomTreeByFilterDTO = new com.hustcad.plm.pdm.partbom.dto.ExpandBomTreeByFilterDTO();
            expandBomTreeByFilterDTO.setAttrFilterCondition(new ArrayList<>());

            ChildPartsFiltersDTO childPartsFiltersDTO = new ChildPartsFiltersDTO();
            childPartsFiltersDTO.setBaseLineOid("");
            childPartsFiltersDTO.setDate("");
            childPartsFiltersDTO.setExpandCondition("latest");
            childPartsFiltersDTO.setLifecycleStateKey("");
            childPartsFiltersDTO.setPurposeOfBom("");
            childPartsFiltersDTO.setSpareBom("");
            childPartsFiltersDTO.setView(partDetailsVO.getView());
            List<ChildPartsFiltersDTO> list = new ArrayList<>();
            list.add(childPartsFiltersDTO);
            expandBomTreeByFilterDTO.setConditions(list);

            expandBomTreeByFilterDTO.setExpandLevel("N");
            expandBomTreeByFilterDTO.setFatherNumber(partDetailsVO.getObjectNumber());
            expandBomTreeByFilterDTO.setFatherOid(String.valueOf(partDetailsVO.getOid()));
            expandBomTreeByFilterDTO.setFatherOtype(partDetailsVO.getOtype());
            expandBomTreeByFilterDTO.setFilterMode("0");
            expandBomTreeByFilterDTO.setGiveUsesOccurrence("N");
            expandBomTreeByFilterDTO.setIbaColumns(new ArrayList<>());
            expandBomTreeByFilterDTO.setIsApplyRoot(false);
            expandBomTreeByFilterDTO.setIsFilterAttr(false);
            expandBomTreeByFilterDTO.setIsNeedTranEnum(true);
            expandBomTreeByFilterDTO.setIsNestedTree(false);
            expandBomTreeByFilterDTO.setIsQueryIBA(true);

            PartBomTreeByFilterVO result = typlmPartBomService.expandBomTreeByFilterNew(
                    expandBomTreeByFilterDTO,
                    Boolean.TRUE);
            for (int y = 0; y < result.getPartBomTreeList().size(); y++) {

                PartBomTree partBomTree = result.getPartBomTreeList().get(y);

                if (statusObj != null) {
                    String statusStr=statusObj.getExpressionText();
                    if(statusStr.equals(partBomTree.getLifecyclestagekey())==false){
                        continue;
                    }
                }
                BigInteger partOid = new BigInteger(partBomTree.getPartoid());
                String oType = partBomTree.getPartotype();
                PartDetailsVO partDetails = typlmPartService.queryPartDetails(partOid, oType);


                if ("ty.inteplm.part.CTyPart".equals(partDetails.getOtype()) == false) {
                    continue;
                }


                if (cmd.containsKey(partBomTree.getObjectNumber())) {
                    if (partBomTree.getOid() == BigInteger.valueOf(0L)) {
                        //获取关联图纸
                        if (mainControlDrawing != null || monControlDrawing != null) {
                            boolean read = false;
                            if (mainControlDrawing != null && "是".equals(mainControlDrawing.getExpressionText())) {
                                read = true;
                            }
                            if (monControlDrawing != null && "是".equals(monControlDrawing.getExpressionText())) {
                                read = true;
                            }
                            if (read) {

                                List<DocDetails> docDetailsList = typlmPdmPartRelObjectService.queryPartRelCadInfo(
                                        new BigInteger(partBomTree.getPartoid()));

                                //List<DocDetails> docDetailsList = typlmPartRelObjectService.queryPartRelatedDocInfo(new BigInteger(partBomTree.getPartoid()));

                                if (statusObj != null) {
                                    String statusStr=statusObj.getExpressionText();
                                    for(int s=0;s<docDetailsList.size();s++){
                                        if(statusStr.equals(docDetailsList.get(s).getLifecycleStateKey())==false){
                                            docDetailsList.remove(s);
                                            s--;
                                        }
                                    }
                                }


                                for (DocDetails docDetails : docDetailsList) {
                                    FlowObject childFlowObject = new FlowObject();
                                    childFlowObject.setRefObjectOid(new BigInteger(docDetails.getOid()));
                                    childFlowObject.setRefObjectOtype(docDetails.getOtype());

                                    CadStructFilterDTO filterDTO = new CadStructFilterDTO();
                                    filterDTO.setLevel("N");
                                    filterDTO.setTopLevel(false);
                                    List<BigInteger> denList=new ArrayList<>();
                                    denList.add(new BigInteger(docDetails.getOid()));
                                    filterDTO.setIdList(denList);

                                    List<CadStructConditionDTO> conditionDTOList=new ArrayList<>();
                                    CadStructConditionDTO cadStructConditionDTO=new CadStructConditionDTO();
                                    cadStructConditionDTO.setQueryType("asstored");
                                    cadStructConditionDTO.setLifecycleState("");
                                    cadStructConditionDTO.setBaseLineOid(null);
                                    conditionDTOList.add(cadStructConditionDTO);
                                    filterDTO.setConditionDTOList(conditionDTOList);

                                    SearchConditionDTO searchCondition=new SearchConditionDTO();
                                    searchCondition.setPageNum(1);
                                    searchCondition.setPageSize(99999999);
                                    searchCondition.setNeedIBA(false);
                                    searchCondition.setNeedThumbnailFile(false);
                                    searchCondition.setSearchType(0);
                                    searchCondition.setColumns(new ArrayList<>());
                                    filterDTO.setSearchCondition(searchCondition);
                                    List<CadStructFilterVO> cadList=typlmCadStructService.queryCadStructByFilterCondition(filterDTO);

                                    if (mainControlDrawing != null && "是".equals(
                                            mainControlDrawing.getExpressionText())) {
                                        if ("主控".equals(docDetails.getBuildRuleName())) {
                                            //docDetailsListAll.add(docDetails);
                                            for(int h=0;h<cadList.size();h++){
                                                CadStructFilterVO cadStructFilterVO=cadList.get(h);

                                                if (statusObj != null) {
                                                    String statusStr=statusObj.getExpressionText();
                                                    if(statusStr.equals(cadStructFilterVO.getResultEntityVo().getLifecyclestagekey())== false){
                                                        continue;
                                                    }
                                                }

                                                DocDetails addDoc=new DocDetails();
                                                addDoc.setBranchid(String.valueOf(cadStructFilterVO.getResultEntityVo().getBranchid()));
                                                addDoc.setOid(String.valueOf(cadStructFilterVO.getResultEntityVo().getOid()));
                                                addDoc.setOtype(cadStructFilterVO.getResultEntityVo().getOtype());
                                                docDetailsListAll.add(addDoc);
                                            }
                                        }
                                    }
                                    if (monControlDrawing != null && "是".equals(
                                            monControlDrawing.getExpressionText())) {
                                        if ("主控".equals(docDetails.getBuildRuleName())==false) {
                                            //docDetailsListAll.add(docDetails);
                                            for(int h=0;h<cadList.size();h++){
                                                CadStructFilterVO cadStructFilterVO=cadList.get(h);
                                                if (statusObj != null) {
                                                    String statusStr=statusObj.getExpressionText();
                                                    if(statusStr.equals(cadStructFilterVO.getResultEntityVo().getLifecyclestagekey())== false){
                                                        continue;
                                                    }
                                                }
                                                DocDetails addDoc=new DocDetails();
                                                addDoc.setBranchId(String.valueOf(cadStructFilterVO.getResultEntityVo().getBranchid()));
                                                addDoc.setOid(String.valueOf(cadStructFilterVO.getResultEntityVo().getOid()));
                                                addDoc.setOtype(cadStructFilterVO.getResultEntityVo().getOtype());
                                                docDetailsListAll.add(addDoc);
                                            }
                                        }
                                    }
                                }

                            }

                        }

                        //获取参考文档
                        if (referencedDocument != null) {
                            if ("是".equals(referencedDocument.getExpressionText())) {
                                List<DocDetails> jsonList = typlmPdmPartService.getPartLinkReferenceDoc(
                                        partBomTree.getPartoid());


                                if (statusObj != null) {
                                    String statusStr=statusObj.getExpressionText();
                                    for(int s=0;s<jsonList.size();s++){
                                        if(statusStr.equals(jsonList.get(s).getLifecycleStateKey())== false){
                                            jsonList.remove(s);
                                            s--;
                                        }
                                    }
                                }


                                if (jsonList.size() > 0) {
                                    referenceDocList.addAll(jsonList);
                                }

                                List<IdentifierEntity> identifierEntities=new ArrayList<>();
                                IdentifierEntity identifierEntity=new IdentifierEntity();
                                identifierEntity.setOid(new BigInteger(partBomTree.getPartoid()));
                                identifierEntity.setOtype(partBomTree.getPartotype());

                                identifierEntities.add(identifierEntity);

                                List<QueryDescribeObjVO> describeList=typlmPartDescribeLinkService.queryPartDescribeDoc(identifierEntities);
                                if(describeList.size()>0){
                                    if (statusObj != null) {
                                        String statusStr=statusObj.getExpressionText();
                                        for(int s=0;s<describeList.size();s++){
                                            QueryDescribeObjVO queryDescribeObjVO=describeList.get(s);
                                            if(queryDescribeObjVO.getDescribeObjList()!=null){
                                                for(int z=0;z<queryDescribeObjVO.getDescribeObjList().size();z++) {
                                                    ResultEntityVO resultEntityVO=queryDescribeObjVO.getDescribeObjList().get(z);
                                                    if(statusStr.equals(resultEntityVO.getLifecyclestagekey())== false) {
                                                        queryDescribeObjVO.getDescribeObjList().remove(z);
                                                        z--;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                    describeObjList.addAll(describeList);
                                }
                            }

                        }
                    }

                    continue;
                }
                if (subComponent != null) {
                    if ("是".equals(subComponent.getExpressionText())) {
                        if (partBomTree.getOid() != BigInteger.valueOf(0L)) {
                            subComponentList.add(partDetails);
                        }
                    }
                }

                //获取关联图纸
                if (mainControlDrawing != null || monControlDrawing != null) {
                    boolean read = false;
                    if (mainControlDrawing != null && "是".equals(mainControlDrawing.getExpressionText())) {
                        read = true;
                    }
                    if (monControlDrawing != null && "是".equals(monControlDrawing.getExpressionText())) {
                        read = true;
                    }
                    if (read) {

                        List<DocDetails> docDetailsList = typlmPdmPartRelObjectService.queryPartRelCadInfo(
                                new BigInteger(partBomTree.getPartoid()));

                        //List<DocDetails> docDetailsList = typlmPartRelObjectService.queryPartRelatedDocInfo(new BigInteger(partBomTree.getPartoid()));

                        if (statusObj != null) {
                            String statusStr=statusObj.getExpressionText();
                            for(int s=0;s<docDetailsList.size();s++){
                                if(statusStr.equals(docDetailsList.get(s).getLifecycleStateKey())== false){
                                    docDetailsList.remove(s);
                                    s--;
                                }
                            }
                        }


                        for (DocDetails docDetails : docDetailsList) {
                            FlowObject childFlowObject = new FlowObject();
                            childFlowObject.setRefObjectOid(new BigInteger(docDetails.getOid()));
                            childFlowObject.setRefObjectOtype(docDetails.getOtype());

                            CadStructFilterDTO filterDTO = new CadStructFilterDTO();
                            filterDTO.setLevel("N");
                            filterDTO.setTopLevel(false);
                            List<BigInteger> denList=new ArrayList<>();
                            denList.add(new BigInteger(docDetails.getOid()));
                            filterDTO.setIdList(denList);

                            List<CadStructConditionDTO> conditionDTOList=new ArrayList<>();
                            CadStructConditionDTO cadStructConditionDTO=new CadStructConditionDTO();
                            cadStructConditionDTO.setQueryType("asstored");
                            cadStructConditionDTO.setLifecycleState("");
                            cadStructConditionDTO.setBaseLineOid(null);
                            conditionDTOList.add(cadStructConditionDTO);
                            filterDTO.setConditionDTOList(conditionDTOList);

                            SearchConditionDTO searchCondition=new SearchConditionDTO();
                            searchCondition.setPageNum(1);
                            searchCondition.setPageSize(99999999);
                            searchCondition.setNeedIBA(false);
                            searchCondition.setNeedThumbnailFile(false);
                            searchCondition.setSearchType(0);
                            searchCondition.setColumns(new ArrayList<>());
                            filterDTO.setSearchCondition(searchCondition);
                            List<CadStructFilterVO> cadList=typlmCadStructService.queryCadStructByFilterCondition(filterDTO);

                            if (mainControlDrawing != null && "是".equals(
                                    mainControlDrawing.getExpressionText())) {
                                if ("主控".equals(docDetails.getBuildRuleName())) {
                                    //docDetailsListAll.add(docDetails);
                                    for(int h=0;h<cadList.size();h++){
                                        CadStructFilterVO cadStructFilterVO=cadList.get(h);
                                        if (statusObj != null) {
                                            String statusStr=statusObj.getExpressionText();
                                            if(statusStr.equals(cadStructFilterVO.getResultEntityVo().getLifecyclestagekey())== false){
                                                continue;
                                            }
                                        }
                                        DocDetails addDoc=new DocDetails();
                                        addDoc.setBranchid(String.valueOf(cadStructFilterVO.getResultEntityVo().getBranchid()));
                                        addDoc.setOid(String.valueOf(cadStructFilterVO.getResultEntityVo().getOid()));
                                        addDoc.setOtype(cadStructFilterVO.getResultEntityVo().getOtype());
                                        docDetailsListAll.add(addDoc);
                                    }
                                }
                            }
                            if (monControlDrawing != null && "是".equals(
                                    monControlDrawing.getExpressionText())) {
                                if ("主控".equals(docDetails.getBuildRuleName())==false) {
                                    //docDetailsListAll.add(docDetails);
                                    for(int h=0;h<cadList.size();h++){
                                        CadStructFilterVO cadStructFilterVO=cadList.get(h);
                                        if (statusObj != null) {
                                            String statusStr=statusObj.getExpressionText();
                                            if(statusStr.equals(cadStructFilterVO.getResultEntityVo().getLifecyclestagekey())== false){
                                                continue;
                                            }
                                        }
                                        DocDetails addDoc=new DocDetails();
                                        addDoc.setBranchid(String.valueOf(cadStructFilterVO.getResultEntityVo().getBranchid()));
                                        addDoc.setOid(String.valueOf(cadStructFilterVO.getResultEntityVo().getOid()));
                                        addDoc.setOtype(cadStructFilterVO.getResultEntityVo().getOtype());
                                        docDetailsListAll.add(addDoc);
                                    }
                                }
                            }
                        }

                    }

                }


                //获取参考文档
                if (referencedDocument != null) {
                    if ("是".equals(referencedDocument.getExpressionText())) {
                        List<DocDetails> jsonList = typlmPdmPartService.getPartLinkReferenceDoc(
                                partBomTree.getPartoid());


                        if (statusObj != null) {
                            String statusStr=statusObj.getExpressionText();
                            for(int s=0;s<jsonList.size();s++){
                                if(statusStr.equals(jsonList.get(s).getLifecycleStateKey())== false){
                                    jsonList.remove(s);
                                    s--;
                                }
                            }
                        }


                        if (jsonList.size() > 0) {
                            referenceDocList.addAll(jsonList);
                        }

                        List<IdentifierEntity> identifierEntities=new ArrayList<>();
                        IdentifierEntity identifierEntity=new IdentifierEntity();
                        identifierEntity.setOid(new BigInteger(partBomTree.getPartoid()));
                        identifierEntity.setOtype(partBomTree.getPartotype());

                        identifierEntities.add(identifierEntity);

                        List<QueryDescribeObjVO> describeList=typlmPartDescribeLinkService.queryPartDescribeDoc(identifierEntities);
                        if(describeList.size()>0){
                            if (statusObj != null) {
                                String statusStr=statusObj.getExpressionText();
                                for(int s=0;s<describeList.size();s++){
                                    QueryDescribeObjVO queryDescribeObjVO=describeList.get(s);
                                    if(queryDescribeObjVO.getDescribeObjList()!=null){
                                        for(int z=0;z<queryDescribeObjVO.getDescribeObjList().size();z++) {
                                            ResultEntityVO resultEntityVO=queryDescribeObjVO.getDescribeObjList().get(z);
                                            if(statusStr.equals(resultEntityVO.getLifecyclestagekey())== false) {
                                                queryDescribeObjVO.getDescribeObjList().remove(z);
                                                z--;
                                            }
                                        }
                                    }
                                }
                            }
                            describeObjList.addAll(describeList);
                        }
                    }

                }
            }
        }

        boolean drawingPartAdd=false;
        if(this.drawingPart!=null){
            if("是".equals(this.drawingPart.getExpressionText())){
                drawingPartAdd=true;
            }
        }
        if(this.afterDrawingPart!=null){
            if("是".equals(this.afterDrawingPart.getExpressionText())){
                drawingPartAdd=true;
            }
        }
        if(drawingPartAdd){
            List<DocDetails> docDetailsList = collectMasterDocDetails(workFlowContainerId);
            for(int i=0;i<docDetailsList.size();i++){
                List<PartDetailsVO> partList=typlmCADRelObjectService.queryCadRefPartInfo(new BigInteger(docDetailsList.get(i).getOid()));
                for(int z=0;z<partList.size();z++){
                    drawingPartList.add(partList.get(z));
                }
            }
        }


        List<BaseObject> baseObjectList = new ArrayList<>();

        for (int a = 0; a < subComponentList.size(); a++) {
            BaseObject baseObject = new BaseObject();
            baseObject.setBranchId(new BigInteger(subComponentList.get(a).getBranchid()));
            baseObject.setOtype(subComponentList.get(a).getOtype());
            baseObject.setOid(new BigInteger(subComponentList.get(a).getOid()));
            baseObjectList.add(baseObject);
        }
        for (int a = 0; a < drawingPartList.size(); a++) {
            BaseObject baseObject = new BaseObject();
            baseObject.setBranchId(new BigInteger(drawingPartList.get(a).getBranchid()));
            baseObject.setOtype(drawingPartList.get(a).getOtype());
            baseObject.setOid(new BigInteger(drawingPartList.get(a).getOid()));
            baseObjectList.add(baseObject);
        }


        for (int a = 0; a < docDetailsListAll.size(); a++) {
            DocDetails entityDocDetails=docDetailsListAll.get(a);
            BaseObject baseObject = new BaseObject();
            baseObject.setBranchId(new BigInteger(entityDocDetails.getBranchid()));
            baseObject.setOtype(entityDocDetails.getOtype());
            baseObject.setOid(new BigInteger(entityDocDetails.getOid()));
            baseObjectList.add(baseObject);
        }
        for (int a = 0; a < referenceDocListAll.size(); a++) {
            BaseObject baseObject = new BaseObject();
            baseObject.setBranchId(new BigInteger(referenceDocListAll.get(a).getString("branchId")));
            baseObject.setOtype(referenceDocListAll.get(a).getString("otype"));
            baseObject.setOid(new BigInteger(referenceDocListAll.get(a).getString("oid")));
            baseObjectList.add(baseObject);
        }

        for (int a = 0; a < referenceDocList.size(); a++) {
            BaseObject baseObject = new BaseObject();
            baseObject.setBranchId(new BigInteger(referenceDocList.get(a).getBranchId()));
            baseObject.setOtype(referenceDocList.get(a).getOtype());
            baseObject.setOid(new BigInteger(referenceDocList.get(a).getOid()));
            baseObjectList.add(baseObject);
        }
        for (int a = 0; a < describeObjList.size(); a++) {
            QueryDescribeObjVO queryDescribeObjVO=describeObjList.get(a);
            for(int z=0;z<queryDescribeObjVO.getDescribeObjList().size();z++){
                ResultEntityVO resultEntityVO=queryDescribeObjVO.getDescribeObjList().get(z);
                BaseObject baseObject = new BaseObject();
                baseObject.setBranchId(resultEntityVO.getBranchid());
                baseObject.setOtype(resultEntityVO.getOtype());
                baseObject.setOid(resultEntityVO.getOid());
                baseObjectList.add(baseObject);
            }
        }


        if (baseObjectList.size() > 0) {
            FlowObjectStructuredParamVO flowObjectStructuredParamVO = new FlowObjectStructuredParamVO();
            flowObjectStructuredParamVO.setWorkFlowId(new BigInteger(workflowId));
            flowObjectStructuredParamVO.setWorkFlowContainerId(workFlowContainerId);
            flowObjectStructuredParamVO.setRootObjList(workFlowObjectPackage.getFlowObjectList());
            flowObjectStructuredParamVO.setOirAllObjList(workFlowObjectPackage.getFlowObjectList());
            flowObjectStructuredParamVO.setObjList(baseObjectList);
            FlowObjectStructuredReturnVO flowObjectStructuredReturnVO = typlmWorkFlowObjectPackageService.addCheckWorkFlowStartCommon(
                    flowObjectStructuredParamVO);

        }

        return 0;

    }

    /**
     * 变更流程收集
     * @param flowChgFormView
     * @param workFlowContainerId
     * @return
     */
    public int changeCollection(FlowChgFormView flowChgFormView, BigInteger workFlowContainerId) {

        TyplmPartBomService typlmPartBomService = SpringUtil.getBean(TyplmPartBomService.class);
        TyplmPartService typlmPartService = SpringUtil.getBean(
                TyplmPartService.class);
        TyplmPdmPartService typlmPdmPartService = SpringUtil.getBean(
                TyplmPdmPartService.class);
        TyplmEngineeringChangeService typlmEngineeringChangeService = SpringUtil.getBean(
                TyplmEngineeringChangeService.class);
        TyplmPartDescribeLinkService typlmPartDescribeLinkService = SpringUtil.getBean(
                TyplmPartDescribeLinkService.class);
        TyplmPartRelObjectService typlmPartRelObjectService = SpringUtil.getBean(TyplmPartRelObjectService.class);
        TyplmCADRelObjectService typlmCADRelObjectService = SpringUtil.getBean(TyplmCADRelObjectService.class);
        TyplmPdmPartRelObjectService typlmPdmPartRelObjectService = SpringUtil.getBean(
                TyplmPdmPartRelObjectService.class);
        TyplmCadStructService typlmCadStructService=SpringUtil.getBean(
                TyplmCadStructService.class);

        Map<String, Object> cmd = new HashMap<>();
        if(flowChgFormView.getChgFormVO()!=null && flowChgFormView.getChgFormVO().getEcAffectVOS()!=null){
            for (int i = 0; i < flowChgFormView.getChgFormVO().getEcAffectVOS().size(); i++) {
                ECObjData ecObjData = flowChgFormView.getChgFormVO().getEcAffectVOS().get(i);
                cmd.put(ecObjData.getObjectNumber(), ecObjData);
            }
        }


        List<PartDetailsVO> partDetailsVOList = collectMasterObject(workFlowContainerId);


        List<DocDetails> docDetailsListAll = new ArrayList<>();
        List<JSONObject> referenceDocListAll = new ArrayList<>();
        List<PartDetailsVO> subComponentList = new ArrayList<>();
        List<DocDetails> referenceDocList = new ArrayList<>();
        List<QueryDescribeObjVO> describeObjList = new ArrayList<>();

        for (int i = 0; i < partDetailsVOList.size(); i++) {
            PartDetailsVO entityPartDetailsVO = partDetailsVOList.get(i);

            com.hustcad.plm.pdm.partbom.dto.ExpandBomTreeByFilterDTO expandBomTreeByFilterDTO = new com.hustcad.plm.pdm.partbom.dto.ExpandBomTreeByFilterDTO();
            expandBomTreeByFilterDTO.setAttrFilterCondition(new ArrayList<>());

            ChildPartsFiltersDTO childPartsFiltersDTO = new ChildPartsFiltersDTO();
            childPartsFiltersDTO.setBaseLineOid("");
            childPartsFiltersDTO.setDate("");
            childPartsFiltersDTO.setExpandCondition("latest");
            childPartsFiltersDTO.setLifecycleStateKey("");
            childPartsFiltersDTO.setPurposeOfBom("");
            childPartsFiltersDTO.setSpareBom("");
            childPartsFiltersDTO.setView(entityPartDetailsVO.getView());
            List<ChildPartsFiltersDTO> list = new ArrayList<>();
            list.add(childPartsFiltersDTO);
            expandBomTreeByFilterDTO.setConditions(list);

            expandBomTreeByFilterDTO.setExpandLevel("N");
            expandBomTreeByFilterDTO.setFatherNumber(entityPartDetailsVO.getObjectNumber());
            expandBomTreeByFilterDTO.setFatherOid(String.valueOf(entityPartDetailsVO.getOid()));
            expandBomTreeByFilterDTO.setFatherOtype(entityPartDetailsVO.getOtype());
            expandBomTreeByFilterDTO.setFilterMode("0");
            expandBomTreeByFilterDTO.setGiveUsesOccurrence("N");
            expandBomTreeByFilterDTO.setIbaColumns(new ArrayList<>());
            expandBomTreeByFilterDTO.setIsApplyRoot(false);
            expandBomTreeByFilterDTO.setIsFilterAttr(false);
            expandBomTreeByFilterDTO.setIsNeedTranEnum(true);
            expandBomTreeByFilterDTO.setIsNestedTree(false);
            expandBomTreeByFilterDTO.setIsQueryIBA(true);

            PartBomTreeByFilterVO result = typlmPartBomService.expandBomTreeByFilterNew(
                    expandBomTreeByFilterDTO,
                    Boolean.TRUE);
            for (int y = 0; y < result.getPartBomTreeList().size(); y++) {

                PartBomTree partBomTree = result.getPartBomTreeList().get(y);

                if (statusObj != null) {
                    String statusStr=statusObj.getExpressionText();
                    if(statusStr.equals(partBomTree.getLifecyclestagekey())== false){
                        continue;
                    }
                }
                BigInteger partOid = new BigInteger(partBomTree.getPartoid());
                String oType = partBomTree.getPartotype();
                PartDetailsVO partDetails = typlmPartService.queryPartDetails(partOid, oType);


                if ("ty.inteplm.part.CTyPart".equals(partDetails.getOtype()) == false) {
                    continue;
                }


                if (cmd.containsKey(partBomTree.getObjectNumber())) {
                    if (partBomTree.getOid() == BigInteger.valueOf(0L)) {
                        //获取关联图纸
                        if (mainControlDrawing != null || monControlDrawing != null) {
                            boolean read = false;
                            if (mainControlDrawing != null && "是".equals(mainControlDrawing.getExpressionText())) {
                                read = true;
                            }
                            if (monControlDrawing != null && "是".equals(monControlDrawing.getExpressionText())) {
                                read = true;
                            }
                            if (read) {

                                List<DocDetails> docDetailsList = typlmPdmPartRelObjectService.queryPartRelCadInfo(
                                        new BigInteger(partBomTree.getPartoid()));

                                //List<DocDetails> docDetailsList = typlmPartRelObjectService.queryPartRelatedDocInfo(new BigInteger(partBomTree.getPartoid()));

                                if (statusObj != null) {
                                    String statusStr=statusObj.getExpressionText();
                                    for(int s=0;s<docDetailsList.size();s++){
                                        if(statusStr.equals(docDetailsList.get(s).getLifecycleStateKey())== false){
                                            docDetailsList.remove(s);
                                            s--;
                                        }
                                    }
                                }


                                for (DocDetails docDetails : docDetailsList) {
                                    FlowObject childFlowObject = new FlowObject();
                                    childFlowObject.setRefObjectOid(new BigInteger(docDetails.getOid()));
                                    childFlowObject.setRefObjectOtype(docDetails.getOtype());

                                    CadStructFilterDTO filterDTO = new CadStructFilterDTO();
                                    filterDTO.setLevel("N");
                                    filterDTO.setTopLevel(false);
                                    List<BigInteger> denList=new ArrayList<>();
                                    denList.add(new BigInteger(docDetails.getOid()));
                                    filterDTO.setIdList(denList);

                                    List<CadStructConditionDTO> conditionDTOList=new ArrayList<>();
                                    CadStructConditionDTO cadStructConditionDTO=new CadStructConditionDTO();
                                    cadStructConditionDTO.setQueryType("asstored");
                                    cadStructConditionDTO.setLifecycleState("");
                                    cadStructConditionDTO.setBaseLineOid(null);
                                    conditionDTOList.add(cadStructConditionDTO);
                                    filterDTO.setConditionDTOList(conditionDTOList);

                                    SearchConditionDTO searchCondition=new SearchConditionDTO();
                                    searchCondition.setPageNum(1);
                                    searchCondition.setPageSize(99999999);
                                    searchCondition.setNeedIBA(false);
                                    searchCondition.setNeedThumbnailFile(false);
                                    searchCondition.setSearchType(0);
                                    searchCondition.setColumns(new ArrayList<>());
                                    filterDTO.setSearchCondition(searchCondition);
                                    List<CadStructFilterVO> cadList=typlmCadStructService.queryCadStructByFilterCondition(filterDTO);

                                    if (mainControlDrawing != null && "是".equals(
                                            mainControlDrawing.getExpressionText())) {
                                        if ("主控".equals(docDetails.getBuildRuleName())) {
                                            //docDetailsListAll.add(docDetails);
                                            for(int h=0;h<cadList.size();h++){
                                                CadStructFilterVO cadStructFilterVO=cadList.get(h);
                                                if (statusObj != null) {
                                                    String statusStr=statusObj.getExpressionText();
                                                    if(statusStr.equals(cadStructFilterVO.getResultEntityVo().getLifecyclestagekey())== false){
                                                        continue;
                                                    }
                                                }
                                                DocDetails addDoc=new DocDetails();
                                                addDoc.setBranchid(String.valueOf(cadStructFilterVO.getResultEntityVo().getBranchid()));
                                                addDoc.setOid(String.valueOf(cadStructFilterVO.getResultEntityVo().getOid()));
                                                addDoc.setOtype(cadStructFilterVO.getResultEntityVo().getOtype());
                                                docDetailsListAll.add(addDoc);
                                            }
                                        }
                                    }
                                    if (monControlDrawing != null && "是".equals(
                                            monControlDrawing.getExpressionText())) {
                                        if ("主控".equals(docDetails.getBuildRuleName())==false) {
                                            //docDetailsListAll.add(docDetails);
                                            for(int h=0;h<cadList.size();h++){
                                                CadStructFilterVO cadStructFilterVO=cadList.get(h);
                                                if (statusObj != null) {
                                                    String statusStr=statusObj.getExpressionText();
                                                    if(statusStr.equals(cadStructFilterVO.getResultEntityVo().getLifecyclestagekey())== false){
                                                        continue;
                                                    }
                                                }
                                                DocDetails addDoc=new DocDetails();
                                                addDoc.setBranchId(String.valueOf(cadStructFilterVO.getResultEntityVo().getBranchid()));
                                                addDoc.setOid(String.valueOf(cadStructFilterVO.getResultEntityVo().getOid()));
                                                addDoc.setOtype(cadStructFilterVO.getResultEntityVo().getOtype());
                                                docDetailsListAll.add(addDoc);
                                            }
                                        }
                                    }
                                }

                            }

                        }

                        //获取参考文档
                        if (referencedDocument != null) {
                            if ("是".equals(referencedDocument.getExpressionText())) {
                                List<DocDetails> jsonList = typlmPdmPartService.getPartLinkReferenceDoc(
                                        partBomTree.getPartoid());

                                if (statusObj != null) {
                                    String statusStr=statusObj.getExpressionText();
                                    for(int s=0;s<jsonList.size();s++){
                                        if(statusStr.equals(jsonList.get(s).getLifecycleStateKey())== false){
                                            jsonList.remove(s);
                                            s--;
                                        }
                                    }
                                }



                                if (jsonList.size() > 0) {
                                    referenceDocList.addAll(jsonList);
                                }

                                List<IdentifierEntity> identifierEntities=new ArrayList<>();
                                IdentifierEntity identifierEntity=new IdentifierEntity();
                                identifierEntity.setOid(new BigInteger(partBomTree.getPartoid()));
                                identifierEntity.setOtype(partBomTree.getPartotype());

                                identifierEntities.add(identifierEntity);

                                List<QueryDescribeObjVO> describeList=typlmPartDescribeLinkService.queryPartDescribeDoc(identifierEntities);
                                if(describeList.size()>0){
                                    if (statusObj != null) {
                                        String statusStr=statusObj.getExpressionText();
                                        for(int s=0;s<describeList.size();s++){
                                            QueryDescribeObjVO queryDescribeObjVO=describeList.get(s);
                                            for(int z=0;z<queryDescribeObjVO.getDescribeObjList().size();z++) {
                                                if(statusStr.equals(queryDescribeObjVO.getDescribeObjList().get(z).getLifecyclestagekey())== false) {
                                                    queryDescribeObjVO.getDescribeObjList().remove(z);
                                                    z--;
                                                }
                                            }
                                        }
                                    }
                                    describeObjList.addAll(describeList);
                                }
                            }

                        }
                    }

                    continue;
                }


                if (subComponent != null) {
                    if ("是".equals(subComponent.getExpressionText())) {
                        if (partBomTree.getOid() != BigInteger.valueOf(0L)) {
                            subComponentList.add(partDetails);
                        }
                    }
                }

                //获取关联图纸
                if (mainControlDrawing != null || monControlDrawing != null) {
                    boolean read = false;
                    if (mainControlDrawing != null && "是".equals(mainControlDrawing.getExpressionText())) {
                        read = true;
                    }
                    if (monControlDrawing != null && "是".equals(monControlDrawing.getExpressionText())) {
                        read = true;
                    }
                    if (read) {

                        List<DocDetails> docDetailsList = typlmPdmPartRelObjectService.queryPartRelCadInfo(
                                new BigInteger(partBomTree.getPartoid()));

                        //List<DocDetails> docDetailsList = typlmPartRelObjectService.queryPartRelatedDocInfo(new BigInteger(partBomTree.getPartoid()));

                        if (statusObj != null) {
                            String statusStr=statusObj.getExpressionText();
                            for(int s=0;s<docDetailsList.size();s++){
                                if(statusStr.equals(docDetailsList.get(s).getLifecycleStateKey())== false){
                                    docDetailsList.remove(s);
                                    s--;
                                }
                            }
                        }


                        for (DocDetails docDetails : docDetailsList) {
                            FlowObject childFlowObject = new FlowObject();
                            childFlowObject.setRefObjectOid(new BigInteger(docDetails.getOid()));
                            childFlowObject.setRefObjectOtype(docDetails.getOtype());

                            CadStructFilterDTO filterDTO = new CadStructFilterDTO();
                            filterDTO.setLevel("N");
                            filterDTO.setTopLevel(false);
                            List<BigInteger> denList=new ArrayList<>();
                            denList.add(new BigInteger(docDetails.getOid()));
                            filterDTO.setIdList(denList);

                            List<CadStructConditionDTO> conditionDTOList=new ArrayList<>();
                            CadStructConditionDTO cadStructConditionDTO=new CadStructConditionDTO();
                            cadStructConditionDTO.setQueryType("asstored");
                            cadStructConditionDTO.setLifecycleState("");
                            cadStructConditionDTO.setBaseLineOid(null);
                            conditionDTOList.add(cadStructConditionDTO);
                            filterDTO.setConditionDTOList(conditionDTOList);

                            SearchConditionDTO searchCondition=new SearchConditionDTO();
                            searchCondition.setPageNum(1);
                            searchCondition.setPageSize(99999999);
                            searchCondition.setNeedIBA(false);
                            searchCondition.setNeedThumbnailFile(false);
                            searchCondition.setSearchType(0);
                            searchCondition.setColumns(new ArrayList<>());
                            filterDTO.setSearchCondition(searchCondition);
                            List<CadStructFilterVO> cadList=typlmCadStructService.queryCadStructByFilterCondition(filterDTO);

                            if (mainControlDrawing != null && "是".equals(
                                    mainControlDrawing.getExpressionText())) {
                                if ("主控".equals(docDetails.getBuildRuleName())) {
                                    //docDetailsListAll.add(docDetails);
                                    for(int h=0;h<cadList.size();h++){
                                        CadStructFilterVO cadStructFilterVO=cadList.get(h);
                                        if (statusObj != null) {
                                            String statusStr=statusObj.getExpressionText();
                                            if(statusStr.equals(cadStructFilterVO.getResultEntityVo().getLifecyclestagekey())== false){
                                                continue;
                                            }
                                        }
                                        DocDetails addDoc=new DocDetails();
                                        addDoc.setBranchid(String.valueOf(cadStructFilterVO.getResultEntityVo().getBranchid()));
                                        addDoc.setOid(String.valueOf(cadStructFilterVO.getResultEntityVo().getOid()));
                                        addDoc.setOtype(cadStructFilterVO.getResultEntityVo().getOtype());
                                        docDetailsListAll.add(addDoc);
                                    }
                                }
                            }
                            if (monControlDrawing != null && "是".equals(
                                    monControlDrawing.getExpressionText())) {
                                if ("主控".equals(docDetails.getBuildRuleName())==false) {
                                    //docDetailsListAll.add(docDetails);
                                    for(int h=0;h<cadList.size();h++){
                                        CadStructFilterVO cadStructFilterVO=cadList.get(h);
                                        if (statusObj != null) {
                                            String statusStr=statusObj.getExpressionText();
                                            if(statusStr.equals(cadStructFilterVO.getResultEntityVo().getLifecyclestagekey())== false){
                                                continue;
                                            }
                                        }
                                        DocDetails addDoc=new DocDetails();
                                        addDoc.setBranchId(String.valueOf(cadStructFilterVO.getResultEntityVo().getBranchid()));
                                        addDoc.setOid(String.valueOf(cadStructFilterVO.getResultEntityVo().getOid()));
                                        addDoc.setOtype(cadStructFilterVO.getResultEntityVo().getOtype());
                                        docDetailsListAll.add(addDoc);
                                    }
                                }
                            }
                        }

                    }

                }


                //获取参考文档
                if (referencedDocument != null) {
                    if ("是".equals(referencedDocument.getExpressionText())) {
                        List<DocDetails> jsonList = typlmPdmPartService.getPartLinkReferenceDoc(
                                partBomTree.getPartoid());

                        if (statusObj != null) {
                            String statusStr=statusObj.getExpressionText();
                            for(int s=0;s<jsonList.size();s++){
                                if(statusStr.equals(jsonList.get(s).getLifecycleStateKey())== false){
                                    jsonList.remove(s);
                                    s--;
                                }
                            }
                        }



                        if (jsonList.size() > 0) {
                            referenceDocList.addAll(jsonList);
                        }

                        List<IdentifierEntity> identifierEntities=new ArrayList<>();
                        IdentifierEntity identifierEntity=new IdentifierEntity();
                        identifierEntity.setOid(new BigInteger(partBomTree.getPartoid()));
                        identifierEntity.setOtype(partBomTree.getPartotype());

                        identifierEntities.add(identifierEntity);

                        List<QueryDescribeObjVO> describeList=typlmPartDescribeLinkService.queryPartDescribeDoc(identifierEntities);
                        if(describeList.size()>0){
                            if (statusObj != null) {
                                String statusStr=statusObj.getExpressionText();
                                for(int s=0;s<describeList.size();s++){
                                    QueryDescribeObjVO queryDescribeObjVO=describeList.get(s);
                                    for(int z=0;z<queryDescribeObjVO.getDescribeObjList().size();z++) {
                                        if(statusStr.equals(queryDescribeObjVO.getDescribeObjList().get(z).getLifecyclestagekey())== false) {
                                            queryDescribeObjVO.getDescribeObjList().remove(z);
                                            z--;
                                        }
                                    }
                                }
                            }
                            describeObjList.addAll(describeList);
                        }
                    }

                }
            }

            List<ECObjDTO> entityList=new ArrayList<>();

            for (int a = 0; a < subComponentList.size(); a++) {
                PartDetailsVO partDetailsVO=subComponentList.get(a);
                ECObjDTO ecObjDTO=new ECObjDTO();
                ecObjDTO.setOid(partDetailsVO.getOid());
                ecObjDTO.setOtype(partDetailsVO.getOtype());
                entityList.add(ecObjDTO);
            }


            for (int a = 0; a < docDetailsListAll.size(); a++) {
                DocDetails docDetails=docDetailsListAll.get(a);
                ECObjDTO ecObjDTO=new ECObjDTO();
                ecObjDTO.setOid(docDetails.getOid());
                ecObjDTO.setOtype(docDetails.getOtype());
                entityList.add(ecObjDTO);
            }
            for (int a = 0; a < referenceDocListAll.size(); a++) {
                ECObjDTO ecObjDTO=new ECObjDTO();
                ecObjDTO.setOid(referenceDocListAll.get(a).getString("otype"));
                ecObjDTO.setOtype(referenceDocListAll.get(a).getString("otype"));
                entityList.add(ecObjDTO);
            }

            for (int a = 0; a < referenceDocList.size(); a++) {
                DocDetails docDetails=referenceDocList.get(a);
                ECObjDTO ecObjDTO=new ECObjDTO();
                ecObjDTO.setOid(docDetails.getOid());
                ecObjDTO.setOtype(docDetails.getOtype());
                entityList.add(ecObjDTO);
            }
            for (int a = 0; a < describeObjList.size(); a++) {
                QueryDescribeObjVO queryDescribeObjVO=describeObjList.get(a);
                for(int z=0;z<queryDescribeObjVO.getDescribeObjList().size();z++){
                    ResultEntityVO resultEntityVO=queryDescribeObjVO.getDescribeObjList().get(z);
                    ECObjDTO ecObjDTO=new ECObjDTO();
                    ecObjDTO.setOid(resultEntityVO.getOid());
                    ecObjDTO.setOtype(resultEntityVO.getOtype());
                    entityList.add(ecObjDTO);
                }
            }

            if (entityList.size() > 0) {
                ECAddChangeObjectDTO addChangeObjectDTO=new ECAddChangeObjectDTO();
                IdentifierEntity entity=new IdentifierEntity();
                entity.setOid(flowChgFormView.getBaseObjectOid());
                entity.setOtype(flowChgFormView.getBaseObjectOtype());
                addChangeObjectDTO.setEntity(entity);

                List<ECObjDTO> rootList=new ArrayList<>();
                ECObjDTO ecObjDTO=new ECObjDTO();
                ecObjDTO.setOid(entityPartDetailsVO.getOid());
                ecObjDTO.setOtype(entityPartDetailsVO.getOtype());
                rootList.add(ecObjDTO);
                addChangeObjectDTO.setRootList(rootList);

                List<ECObjDTO> oirAllObjList=new ArrayList<>();
                oirAllObjList.add(ecObjDTO);
                addChangeObjectDTO.setOirAllObjList(oirAllObjList);


                addChangeObjectDTO.setEntityList(entityList);
                if("变更后".equals(this.addPosition.getExpressionText())){
                    EcAfterAddReturnDTO returnDTO = typlmEngineeringChangeService.addAfterChangeObject(addChangeObjectDTO);
                }
                else if("变更前".equals(this.addPosition.getExpressionText())){
                    List<ECObjDataVO> ecObjDataVOS = typlmEngineeringChangeService.addBeforeChangeObjectWithStrategy(addChangeObjectDTO);
                }

            }
        }

        List<PartDetailsVO> drawingPartList = new ArrayList<>();
        if("是".equals(this.drawingPart.getExpressionText())||"是".equals(this.afterDrawingPart.getExpressionText())){
            List<DocDetails> docDetailsList = collectMasterDocDetails(workFlowContainerId);
            for(int i=0;i<docDetailsList.size();i++){
                List<PartDetailsVO> partList=typlmCADRelObjectService.queryCadRefPartInfo(new BigInteger(docDetailsList.get(i).getOid()));
                for(int z=0;z<partList.size();z++){
                    drawingPartList.add(partList.get(z));
                }
            }
        }

        for (int a = 0; a < drawingPartList.size(); a++) {
            PartDetailsVO entityPartDetailsVO=drawingPartList.get(a);
            List<ECObjDTO> entityList=new ArrayList<>();

            boolean add=true;
            if(this.afterDrawingPart!=null){
                if("是".equals(this.afterDrawingPart.getExpressionText())){
                    if(flowChgFormView.getChgFormVO().getEcAffectVOS()!=null){
                        for (int s = 0; s < flowChgFormView.getChgFormVO().getEcAffectVOS().size(); s++) {
                            ECObjData ecObjData = flowChgFormView.getChgFormVO().getEcAffectVOS().get(s);
                            if(ecObjData.getObjectNumber().equals(entityPartDetailsVO.getObjectNumber())){
                                add=false;
                                break;
                            }
                        }
                    }
                }
            }
            if(this.drawingPart!=null){
                if("是".equals(this.drawingPart.getExpressionText())){
                    if(flowChgFormView.getChgFormVO().getEcObjDataVOS()!=null){
                        for (int s = 0; s < flowChgFormView.getChgFormVO().getEcObjDataVOS().size(); s++) {
                            ECObjData ecObjData = flowChgFormView.getChgFormVO().getEcObjDataVOS().get(s);
                            if(ecObjData.getObjectNumber().equals(entityPartDetailsVO.getObjectNumber())){
                                add=false;
                                break;
                            }
                        }
                    }
                }
            }

            if(add){
                ECObjDTO ecObjDTO=new ECObjDTO();
                ecObjDTO.setIdentifierRef(null);
                ecObjDTO.setOid(entityPartDetailsVO.getOid());
                ecObjDTO.setOtype(entityPartDetailsVO.getOtype());
                entityList.add(ecObjDTO);
            }
            if (entityList.size() > 0) {

                if("变更后".equals(this.addPosition.getExpressionText())){
                    ECAddChangeObjectDTO addChangeObjectDTO=new ECAddChangeObjectDTO();
                    IdentifierEntity entity=new IdentifierEntity();
                    entity.setOid(flowChgFormView.getBaseObjectOid());
                    entity.setOtype(flowChgFormView.getBaseObjectOtype());
                    addChangeObjectDTO.setEntity(entity);

                    List<ECObjDTO> rootList=new ArrayList<>();
                    ECObjDTO ecObjDTO=new ECObjDTO();
                    ecObjDTO.setOid(entityPartDetailsVO.getOid());
                    ecObjDTO.setOtype(entityPartDetailsVO.getOtype());
                    rootList.add(ecObjDTO);
                    addChangeObjectDTO.setRootList(rootList);

                    List<ECObjDTO> oirAllObjList=new ArrayList<>();
                    oirAllObjList.add(ecObjDTO);
                    addChangeObjectDTO.setOirAllObjList(oirAllObjList);


                    addChangeObjectDTO.setEntityList(entityList);
                    EcAfterAddReturnDTO returnDTO = typlmEngineeringChangeService.addAfterChangeObject(addChangeObjectDTO);
                }
                else if("变更前".equals(this.addPosition.getExpressionText())){
                    ECAddChangeObjectDTO addChangeObjectDTO=new ECAddChangeObjectDTO();

                    addChangeObjectDTO.setBefore(true);
                    IdentifierEntity entity=new IdentifierEntity();
                    entity.setOid(flowChgFormView.getBaseObjectOid());
                    entity.setOtype(flowChgFormView.getBaseObjectOtype());
                    addChangeObjectDTO.setEntity(entity);

                    ECObjDTO ecObjDTO=new ECObjDTO();
                    ecObjDTO.setOid(entityPartDetailsVO.getOid());
                    ecObjDTO.setOtype(entityPartDetailsVO.getOtype());

                    addChangeObjectDTO.setEntityList(entityList);
                    typlmEngineeringChangeService.addBeforeChangeObjectWithStrategy(addChangeObjectDTO);
                }
            }

        }



        return 0;
    }
}
